473,435 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,435 software developers and data experts.

why encodeURIComponent() for data in body of POST XHR request?

Hi,

All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding is
required in the case of a GET request and the form data is attached as
a URI query string; however, why is the encoding necessary for POST
requests?

Thanks,
Peter

Aug 2 '07 #1
10 15623
On Aug 2, 12:43 am, Peter Michaux <petermich...@gmail.comwrote:
Hi,

All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding is
required in the case of a GET request and the form data is attached as
a URI query string; however, why is the encoding necessary for POST
requests?

Thanks,
Peter
URL encoding is simply the standard for name-value pairs in an HTTP
request. GET or POST doesn't enter into it (the pairs look exactly
the same in a POST request as they do when tacked onto the query
string for a GET.)

For example, imagine what would happen if you didn't encode the pairs
and one of the values had an ampersand in it.

Aug 2 '07 #2
David Mark wrote on 02 aug 2007 in comp.lang.javascript:
On Aug 2, 12:43 am, Peter Michaux <petermich...@gmail.comwrote:
>>
All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding is
required in the case of a GET request and the form data is attached as
a URI query string; however, why is the encoding necessary for POST
requests?

URL encoding is simply the standard for name-value pairs in an HTTP
request. GET or POST doesn't enter into it (the pairs look exactly
the same in a POST request as they do when tacked onto the query
string for a GET.)
For example, imagine what would happen if you didn't encode the pairs
and one of the values had an ampersand in it.
When using POST, nothing would, I would imagine.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 2 '07 #3
On Aug 2, 3:18 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
David Mark wrote on 02 aug 2007 in comp.lang.javascript:
On Aug 2, 12:43 am, Peter Michaux <petermich...@gmail.comwrote:
All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding is
required in the case of a GET request and the form data is attached as
a URI query string; however, why is the encoding necessary for POST
requests?
URL encoding is simply the standard for name-value pairs in an HTTP
request. GET or POST doesn't enter into it (the pairs look exactly
the same in a POST request as they do when tacked onto the query
string for a GET.)
For example, imagine what would happen if you didn't encode the pairs
and one of the values had an ampersand in it.

When using POST, nothing would, I would imagine.
What does that mean? You can certainly POST form data with ampersands
and they are normally encoded the same way as GET requests.
Aug 2 '07 #4
David Mark wrote on 02 aug 2007 in comp.lang.javascript:
On Aug 2, 3:18 am, "Evertjan." <exjxw.hannivo...@interxnl.netwrote:
>David Mark wrote on 02 aug 2007 in comp.lang.javascript:
On Aug 2, 12:43 am, Peter Michaux <petermich...@gmail.comwrote:
>All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding
is required in the case of a GET request and the form data is
attached as a URI query string; however, why is the encoding
necessary for POST requests?
URL encoding is simply the standard for name-value pairs in an HTTP
request. GET or POST doesn't enter into it (the pairs look exactly
the same in a POST request as they do when tacked onto the query
string for a GET.)
For example, imagine what would happen if you didn't encode the
pairs and one of the values had an ampersand in it.

When using POST, nothing would, I would imagine.
What does that mean?
Eh?
Nothing extaordinary would happen.
You can certainly POST form data with ampersands
and they are normally encoded the same way as GET requests.
With a simple querystring encoding is not automatic.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 2 '07 #5
Peter Michaux wrote:
All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding is
required in the case of a GET request and the form data is attached as
a URI query string; however, why is the encoding necessary for POST
requests?
Because POST uses the same characters to split ('&' and '=') by
default. The method doesn't matter; the data is just offered in
another part of the request.

GET looks like this (header):

GET /file.html?name=Bart&nr=4 HTTP/1.0

while default POST looks like this (body):

POST /file.html HTTP/1.0
Content-length: 15

name=Bart&nr=4

It's like in email conventions, where stuff can be stored in header or
in body.

You can see this mechanism very well in Perl's CGI processing, where
GET reads the input from the URL (a line in the header of the
request), while POST takes it from <STDIN(body of request). The
further processing is identical; and the URL decoding is an important
step here.

I've seen the term "un-webify" for this kind of processing rather than
"URL-decoding"; the latter indeed sounds like it refers to GET only.

But the encoding of POST-ed data is only the default behaviour of the
browser (which is done in Ajax "by hand" in such libraries).

<form method="post">

actually means:

<form method="post"
enctype="application/x-www-form-urlencoded">

But it's possible to disable this URL-encoding for POST-ed data,
mostly to transfer (binary) files to the gateway software. In the
following example, you tell the form not to encode anything:

<form method="post" enctype="multipart/form-data">

This is the only case where 'é' will be passed as 'é'; under default
GET/POST rules it will always be passed as '%E9'.

Again, this is identical to email; all mail attachments are sent using
multipart (but, unlike HTTP, they are additionally base64-encoded).

So as a general conclusion: Ajax libraries must invoke
encodeURIComponent() when sending POST-requests in the "application/x-
www-form-urlencoded" encoding type. They must always invoke
encodeURIComponent() for GET. They must not invoke
encodeURIComponent() in the POST "multipart/form-data" type.

Obviously, it also depends on how the gateway software is configured
how to handle incoming data (this will be URL decoded by default).

Hope this helps,

--
Bart

Aug 2 '07 #6
"Evertjan." wrote:

David Mark wrote:
>You can certainly POST form data with ampersands
and they are normally encoded the same way as GET requests.

With a simple querystring encoding is not automatic.
URL-encoding is 'automatic' in browsers as long as you don't override
this setting. POST or GET doesn't matter; browser will URL-encode all
form values by default before sending them to server.

If you don't want this to happen, you should use

<form method="post" enctype="multipart/form-data">

to receive non-encoded data at server.

--
Bart

Aug 2 '07 #7
On Aug 1, 10:19 pm, David Mark <dmark.cins...@gmail.comwrote:
On Aug 2, 12:43 am, Peter Michaux <petermich...@gmail.comwrote:
Hi,
All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding is
required in the case of a GET request and the form data is attached as
a URI query string; however, why is the encoding necessary for POST
requests?
Thanks,
Peter

URL encoding is simply the standard for name-value pairs in an HTTP
request. GET or POST doesn't enter into it (the pairs look exactly
the same in a POST request as they do when tacked onto the query
string for a GET.)

For example, imagine what would happen if you didn't encode the pairs
and one of the values had an ampersand in it.
Thanks, David. Now it is obvious using '&' as a separator implies the
keys and values can't have '&'. I've let myself be tripped up by
thinking these requests are more magical than just text transfer
before.

Peter

Aug 2 '07 #8
On Aug 2, 2:39 am, Bart Van der Donck <b...@nijlen.comwrote:
Peter Michaux wrote:
All Ajax libraries I've read use encodeURIComponent() on the name-
value pairs extracted from forms before POST ing the result to the
server with and xmlhttprequest. I can understand why this encoding is
required in the case of a GET request and the form data is attached as
a URI query string; however, why is the encoding necessary for POST
requests?

Because POST uses the same characters to split ('&' and '=') by
default. The method doesn't matter; the data is just offered in
another part of the request.

GET looks like this (header):

GET /file.html?name=Bart&nr=4 HTTP/1.0

while default POST looks like this (body):

POST /file.html HTTP/1.0
Content-length: 15

name=Bart&nr=4

It's like in email conventions, where stuff can be stored in header or
in body.

You can see this mechanism very well in Perl's CGI processing, where
GET reads the input from the URL (a line in the header of the
request), while POST takes it from <STDIN(body of request). The
further processing is identical; and the URL decoding is an important
step here.

I've seen the term "un-webify" for this kind of processing rather than
"URL-decoding"; the latter indeed sounds like it refers to GET only.

But the encoding of POST-ed data is only the default behaviour of the
browser (which is done in Ajax "by hand" in such libraries).

<form method="post">

actually means:

<form method="post"
enctype="application/x-www-form-urlencoded">

But it's possible to disable this URL-encoding for POST-ed data,
mostly to transfer (binary) files to the gateway software. In the
following example, you tell the form not to encode anything:

<form method="post" enctype="multipart/form-data">

This is the only case where 'é' will be passed as 'é'; under default
GET/POST rules it will always be passed as '%E9'.

Again, this is identical to email; all mail attachments are sent using
multipart (but, unlike HTTP, they are additionally base64-encoded).

So as a general conclusion: Ajax libraries must invoke
encodeURIComponent() when sending POST-requests in the "application/x-
www-form-urlencoded" encoding type. They must always invoke
encodeURIComponent() for GET. They must not invoke
encodeURIComponent() in the POST "multipart/form-data" type.

Obviously, it also depends on how the gateway software is configured
how to handle incoming data (this will be URL decoded by default).

Hope this helps,
It helps a lot. Thanks.

Peter

Aug 2 '07 #9
Bart Van der Donck wrote on 02 aug 2007 in comp.lang.javascript:
"Evertjan." wrote:

David Mark wrote:
>>You can certainly POST form data with ampersands
and they are normally encoded the same way as GET requests.

With a simple querystring encoding is not automatic.

URL-encoding is 'automatic' in browsers as long as you don't override
this setting. POST or GET doesn't matter; browser will URL-encode all
form values by default before sending them to server.

If you don't want this to happen, you should use

<form method="post" enctype="multipart/form-data">

to receive non-encoded data at server.
I wrote:
>With a simple querystring encoding is not automatic.
var q = 'text=me&you'
location.href = 'http://cnn.com/?' + q

will be encoded?
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 2 '07 #10
"Evertjan." wrote:
Bart Van der Donck wrote on 02 aug 2007 in comp.lang.javascript:
>URL-encoding is 'automatic' in browsers as long as you don't
override this setting. POST or GET doesn't matter; browser will
URL-encode all form values by default before sending them to
server.
If you don't want this to happen, you should use
<form method="post" enctype="multipart/form-data">
to receive non-encoded data at server.
var q = 'text=me&you'
location.href = 'http://cnn.com/?'+ q

will be encoded?
No, that's why Ajax libraries should use encodeURIComponent() here :)

--
Bart

Aug 2 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Pavils Jurjans | last post by:
Hallo, I am working on multilingual web-application, and I have to be very sure about how the international characters are encoded and decoded in the client-server form requests. There's a...
5
by: fochie | last post by:
Greetings, I'm trying to send data to my server using xmlhttp POST. The data being sent is actually an HTML page that is built with javascript in the browser. The HTML code contains a small...
2
by: Robert Oschler | last post by:
I am working on a PHP 4 app that interacts with an external authorization server. The external server does "third-party" authorization of users. So I do the following: 1) Each of my PHP scripts...
4
by: James Johnson | last post by:
Dear C#Dex, I am trying to automate a POST to a web page that clicks a button. I have been able to hit a target web page and run the web page. However, the button on the page does not click. ...
1
by: Gregory Piñero | last post by:
Hi guys, I'm trying to write a program to get FedEx shipping rates, and I'm following their API as outlined in this document: http://www.fedex.com/us/solutions/wis/pdf/fsm_directmanual.pdf I...
4
by: figelwump | last post by:
All, I am writing an HTTP 1.1 compliant proxy in c#.NET, for use as a proxy server for any web browser (IE, firefox, etc). I've got it working fine for GET requests. However, when a POST...
2
by: MDANH2002 | last post by:
Hi From VB.NET I want to simulate the POST request of the following HTML form <html> <title>HTTP Post Testing</title> <body> <form action=http://www.example.com/postdata ...
2
by: ajaxcoder | last post by:
Hi In my project i had a login form and i am trying to send the username and password to the server for authentication using xmlHttpRequest. Hence i am using POST request but i am unable to send...
1
by: VagabondSW | last post by:
I am working on a project where my client-side Windows application sends a querystring to a URL on a server hosted in New York. The landing page on that server simply redirects the client to...
3
by: Jag | last post by:
Hi I am facing a strange issue. I have 3 ASP pages in the default website 1. auth.aspx <html> <body>
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.